<template>
    <div class="code-display-container">
        <div class="header">
            <h2>{{ meta.title }}</h2>
            <p class="subtitle">您的验证码如下</p>
        </div>

        <div class="code-display">
            <div class="code-value">{{ code }}</div>
            <el-button type="primary" class="copy-btn" @click="copyToClipboard" :icon="CopyDocument">
                复制验证码
            </el-button>
        </div>

        <div class="tips">
            <p>请妥善保管此验证码，不要泄露给他人</p>
            <p>验证码将在1分钟后失效</p>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { CopyDocument } from '@element-plus/icons-vue'

const route = useRoute()

const meta = {
    title: '登录验证码'
}

// 从路由参数中获取验证码（这里假设路由参数id就是验证码）
const code = ref(route.params.id as string || '')

// 复制到剪贴板
const copyToClipboard = async () => {
    try {
        await navigator.clipboard.writeText(code.value)
        ElMessage.success('验证码已复制到剪贴板')
    } catch (err) {
        const textarea = document.createElement('textarea')
        textarea.value = code.value
        document.body.appendChild(textarea)
        textarea.select()
        document.execCommand('copy')
        document.body.removeChild(textarea)
        ElMessage.success('验证码已复制到剪贴板')
    }
}

// 页面加载时检查验证码是否有效
onMounted(() => {
    if (!code.value) {
        ElMessage.error('无效的验证码')
    }
})
</script>

<style scoped>
.code-display-container {
    padding: 20px;
    max-width: 500px;
    margin: 0 auto;
    text-align: center;
}

.header {
    margin-bottom: 30px;
}

.header h2 {
    font-size: 24px;
    color: #333;
    margin-bottom: 8px;
}

.subtitle {
    font-size: 14px;
    color: #666;
}

.code-display {
    background-color: #f5f7fa;
    border-radius: 8px;
    padding: 20px;
    margin: 30px 0;
}

.code-value {
    font-size: 32px;
    font-weight: bold;
    letter-spacing: 5px;
    color: #409eff;
    margin-bottom: 20px;
    word-break: break-all;
}

.copy-btn {
    width: 100%;
    height: 44px;
}

.tips {
    margin-top: 30px;
    font-size: 12px;
    color: #999;
    line-height: 1.6;
}
</style>